package iscript.uiFramework.components.panel
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.event.PickColorEvent;
	
	
	[Event(name = "pick", type = "iscript.uiFramework.event.PickColorEvent")]
	
	public class ColorPickerPanel extends UIComponent 
	{
		private var pickerBd:BitmapData;
		public function ColorPickerPanel()
		{
			pickerBd = new BitmapData(100, 100, false);
			
			addEventListener(MouseEvent.CLICK, _pickColorLogic);
		}
		
		private function _pickColorLogic(e:MouseEvent):void 
		{	
			if(isActive){
				var event:PickColorEvent = new PickColorEvent(PickColorEvent.PICK);
					event.color = pickerBd.getPixel(this.mouseX, this.mouseY);
				dispatchEvent(event);
			}
		}
		
		
		override public function set width(value:Number):void 
		{
			super.width = value;
			pickerBd = new BitmapData(value, pickerBd.height, false);
		}
		
		override public function set height(value:Number):void 
		{
			super.height = value;
			pickerBd = new BitmapData(pickerBd.width, value, false);
		}
		
		
		override public function render(container:DisplayObjectContainer = null):void 
		{
			drawPicker(pickerBd.width,pickerBd.height);
			super.render(container);
		}
		
		
		override public function dispose():Boolean 
		{
			if (super.dispose()) {
				  removeEventListener(MouseEvent.CLICK, _pickColorLogic);
				  pickerBd.dispose();
				}
			return true;
		}
		
		
		private function drawPicker(pickerWidth:Number,pickerHeight:Number):void
		{
			
			var pickerBmp:Bitmap = new Bitmap();
			
			var container:Sprite = new Sprite();
			
			var rainbow:Shape = new Shape();
			
			container.addChild(rainbow);
			
			var shadeBlack:Shape = new Shape();
			
			container.addChild(shadeBlack);
			
			shadeBlack.y = pickerHeight / 2;
			
			var shadeWhite:Shape = new Shape();
			
			container.addChild(shadeWhite);
			
			var grayscale:Shape = new Shape();
			
			container.addChild(grayscale);
			
			grayscale.y = pickerHeight;
			
			var mat:Matrix;
			
			var colors:Array;
			
			var alphas:Array;
			
			var ratios:Array;
			
			mat = new Matrix();
			
			colors = [0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF];
			
			alphas = [1, 1, 1, 1, 1, 1];
			
			ratios = [5, 51, 102, 153, 204, 250];
			
			mat.createGradientBox(pickerWidth, pickerHeight);
			
			rainbow.graphics.lineStyle();
			
			rainbow.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, mat);
			
			rainbow.graphics.drawRect(0, 0, pickerWidth, pickerHeight);
			
			rainbow.graphics.endFill();
			
			mat = new Matrix();
			
			colors = [0x000000, 0x000000];
			
			alphas = [1, 0];
			
			ratios = [0, 255];
			
			mat.createGradientBox(pickerWidth, pickerHeight / 2, toRad(-90));
			
			shadeBlack.graphics.lineStyle();
			
			shadeBlack.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, mat);
			
			shadeBlack.graphics.drawRect(0, 0, pickerWidth, pickerHeight / 2);
			
			shadeBlack.graphics.endFill();
			
			mat = new Matrix();
			
			colors = [0xFFFFFF, 0xFFFFFF];
			
			alphas = [1, 0];
			
			ratios = [0, 255];
			
			mat.createGradientBox(pickerWidth, pickerHeight / 2, toRad(90));
			
			shadeWhite.graphics.lineStyle();
			
			shadeWhite.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, mat);
			
			shadeWhite.graphics.drawRect(0, 0, pickerWidth, pickerHeight / 2);
			
			shadeWhite.graphics.endFill();
			
			pickerBd.draw(container);
			
			pickerBmp.bitmapData = pickerBd;
			
			this.addChild(pickerBmp);
		
		}
		
		private function toRad(a:Number):Number
		{
			
			return a * Math.PI / 180;
		
		}
	
	}

}